package com.ssy.lingxi.contract.esign.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.ssy.lingxi.contract.esign.EsignProperties;
import com.ssy.lingxi.contract.esign.constant.RequestMethod;
import com.ssy.lingxi.contract.esign.exception.EsignException;
import com.ssy.lingxi.contract.esign.request.*;
import com.ssy.lingxi.contract.esign.response.OrgAuthResponse;
import com.ssy.lingxi.contract.esign.response.SignUrlDetailResponse;
import com.ssy.lingxi.contract.esign.response.SubbranchDetailResponse;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 企业认证API
 * @author yzc
 * @version 2.0.0
 * @date 2020/9/8
 */
public class OrganizationAuth {

    private final AuthToken authToken;
    private final EsignProperties properties;

    public OrganizationAuth(AuthToken authToken, EsignProperties properties) {
        this.authToken = authToken;
        this.properties = properties;
    }

    /**
     * 发起企业实名认证4要素校验
     */
    public OrgAuthResponse realName4Auth(OrgCheck4AuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/organization/enterprise/{accountId}/fourFactors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, OrgAuthResponse.class);
    }

    /**
     * 发起企业核身认证4要素校验
     */
    public OrgAuthResponse check4Auth(OrgRealName4AuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/organization/enterprise/fourFactors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
        return JSONUtil.toBean(execute, OrgAuthResponse.class);
    }

    /**
     * 发起企业核身认证3要素校验
     */
    public OrgAuthResponse check3Auth(OrgCheck3AuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/organization/threeFactors";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
        return JSONUtil.toBean(execute, OrgAuthResponse.class);
    }

    //=============================打款认证=================================

    /**
     * 查询打款银行信息
     */
    public SubbranchDetailResponse subbranchDetail(String flowId, String keyWord) throws EsignException {
        String resourcePath = "/v2/identity/auth/pub/organization/{flowId}/subbranch";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("keyWord", keyWord);
        paramMap.put("flowId", flowId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, SubbranchDetailResponse.class);
    }

    /**
     * 发起随机金额打款认证
     */
    public void transferRandomAmount(TransferRandomAmountRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/pub/organization/{flowId}/transferRandomAmount";
        RequestMethod method = RequestMethod.PUT;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 随机金额校验
     */
    public void verifyRandomAmount(String flowId, BigDecimal amount) throws EsignException {
        String resourcePath = "/v2/identity/auth/pub/organization/{flowId}/verifyRandomAmount";
        RequestMethod method = RequestMethod.PUT;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("flowId", flowId);
        paramMap.put("amount", amount);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    //=============================法人认证=================================

    /**
     * 发起授权签署实名认证
     */
    public void legalRepSignRealNameAuth(LegalRepSignRealNameAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/organization/{flowId}/legalRepSignAuth";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 发起授权签署核身认证
     */
    public void legalRepSignCheckAuth(LegalRepSignCheckAuthRequest request) throws EsignException {
        String resourcePath = "/v2/identity/auth/api/organization/{flowId}/legalRepSign";
        RequestMethod method = RequestMethod.POST;

        Map<String, Object> paramMap = BeanUtil.beanToMap(request);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);
    }

    /**
     * 获取授权签署链接
     */
    public SignUrlDetailResponse signUrlDetail(String flowId) throws EsignException {
        String resourcePath = "/v2/identity/auth/pub/organization/{flowId}/signUrl";
        RequestMethod method = RequestMethod.GET;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("flowId", flowId);

        EsignRequest esignRequest = new EsignRequest(method, resourcePath, paramMap, authToken, properties);
        String execute = EsignResource.execute(esignRequest);

        return JSONUtil.toBean(execute, SignUrlDetailResponse.class);
    }
}
